package com.weixing.oauth2.common.oauth2.integration.authenticator;

import com.weixing.oauth2.common.oauth2.integration.IntegrationAuthentication;
import com.weixing.oauth2.common.userdetails.IUserDetailsService;
import com.weixing.oauth2.common.userdetails.SecurityUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LIQIU
 * @date 2018-4-4
 **/
public abstract class AbstractPreparableIntegrationAuthenticator implements IntegrationAuthenticator{

    @Override
    public abstract UserDetails authenticate(IntegrationAuthentication integrationAuthentication, HttpServletRequest request);

    @Override
    public abstract void prepare(IntegrationAuthentication integrationAuthentication, HttpServletRequest request);

    @Override
    public abstract boolean support(IntegrationAuthentication integrationAuthentication);

    @Override
    public abstract void complete(IntegrationAuthentication integrationAuthentication, HttpServletRequest request) ;


    public void complete(IntegrationAuthentication integrationAuthentication, HttpServletRequest request, IUserDetailsService userDetailsService) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (null != authentication && authentication.isAuthenticated() && principal instanceof SecurityUser){
            // 登录成功日志处理
            userDetailsService.handlerLoginData(request,(SecurityUser) principal);
        }
    }
}
